//
//  BaseController.swift
//  microblog
//
//  Created by cy on 2017/8/26.
//  Copyright © 2017年 Aisino. All rights reserved.
//

import UIKit

// MARK: -
class BaseController: UIViewController {
    
    /// 用户登录标记
    var isLogin = true
    
    /// 访客视图信息字典
    var visitorInfoDictionary: [String: String]?
    
    var tableView: UITableView?
    var refreshControl : UIRefreshControl?
    var isPullup = false  // 上拉刷新标记
    
    
    /// 自定义导航栏
    lazy var navBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: UIScreen.cz_screenWidth(), height: 64))
    
    lazy var navItem = UINavigationItem()
    
    override var title: String? {
        didSet {
            navItem.title = title
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
        
        loadData()
    }
    
    /// 加载数据，子类实现
    func loadData() {
        // 如果子类不实现该方法，默认关闭刷新控件
        refreshControl?.endRefreshing()
    }
}


// MARK: - 监听事件
extension BaseController {

    @objc fileprivate func login() {
        print("登录")
    }
    
    @objc fileprivate func register() {
        print("注册")
    }
}


// MARK: - 设置界面
extension BaseController {
    
    fileprivate func setupUI() {
        view.backgroundColor = UIColor.white
        
        // 取消自动缩进 - 如果隐藏了导航栏，会缩进20个点
        automaticallyAdjustsScrollViewInsets = false
        
        setupNavBar()
        isLogin ? setupTableView() : setupVisitorView()
    }
    
    func setupVisitorView() {
        let visitorView = VisitorView(frame: view.bounds)
        view.insertSubview(visitorView, belowSubview: navBar)
        
        // 设置访客视图信息
        visitorView.visitorInfo = visitorInfoDictionary
        
        visitorView.loginButton.addTarget(self, action: #selector(login), for: .touchUpInside)
        visitorView.registerButton.addTarget(self, action: #selector(register), for: .touchUpInside)
        
        // 3. 设置导航条按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(register))
        navItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(login))
    }
    
    func setupTableView() {
        tableView = UITableView(frame: view.bounds, style: .plain)
        view.insertSubview(tableView!, belowSubview: navBar)
        
        tableView?.delegate = self
        tableView?.dataSource = self
        
        tableView?.contentInset = UIEdgeInsets(top: navBar.bounds.height,
                                               left: 0,
                                               bottom: tabBarController?.tabBar.bounds.height ?? 49,
                                               right: 0)
        
        // 设置刷新控件
        refreshControl = UIRefreshControl()
        tableView?.addSubview(refreshControl!)
        refreshControl?.addTarget(self, action: #selector(loadData), for: .valueChanged)
    }
    
    func setupNavBar() {
        view.addSubview(navBar)
        navBar.items = [navItem]
        
        navBar.barTintColor = UIColor.cz_color(withHex: 0xF6F6F6)
        navBar.tintColor = UIColor.darkGray
        navBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.black]
    }
}


// MARK: - UITableViewDataSource, UITableViewDelegate
extension BaseController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        // 判断是否是最后一行，如果是最后一行，下拉刷新
        let row = indexPath.row
        let section = tableView.numberOfSections - 1
        if row < 0 || section < 0 {
            return
        }
        
        let count = tableView.numberOfRows(inSection: section) // 行数
        if row == (count - 1) && !isPullup {
            print("上拉刷新")
            
            // 开始刷新
            isPullup = true
            loadData()
        }
    }
}


